import os
import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from win32com.client import Dispatch, DispatchEx
import win32com
import win32com.client
import win32api,win32con
import web_operation

class login:
    def login_politics(browser,Constant,chrome_options):
        num = 0
        while num < 2:
            try:
                print("打开浏览器")
                Constant.error = ""
                browser.maximize_window()
                browser.get('https://www.news.cn/')
                browser.implicitly_wait(5)
                ##打开时政页面
                browser.find_element(By.XPATH,"/html/body/div[1]/div/div[2]/div/div[3]/div[1]/a[3]").click()
                web_operation.switch_tabs_by_title('时政_新华网',browser)
                ##打开时政联播页面
                browser.find_element(By.XPATH,"/html/body/div[2]/div[3]/div[1]/ul/li/a").click()
                web_operation.switch_tabs_by_title('时政_新华网',browser)
                ##打开学习进行时
                browser.find_element(By.XPATH,"/html/body/div[2]/div[2]/ul[1]/li[1]/a").click()
                web_operation.switch_tabs_by_title('学习进行时-新华网',browser)                                             
                num = 3

                return browser,Constant.error
            
            except Exception as e:
                Constant.error = "进入时政页面失败 ---- " +  repr(e)
                num = num + 1
                browser.quit()
                if num == 1:
                    browser = webdriver.Chrome(options = chrome_options)
        if num == 2:
            return browser,Constant.error

# def _init_(self):
#     login.login_politics()